import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {ParamConfig} from '../common/param.config';
import 'rxjs/add/operator/toPromise';
import {AddNews, AddNewsPage} from './news';
import {Router} from '@angular/router';

@Injectable()
export class AddNewsService {
  public token: string;
  private getNewhostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'ofWeb/news/list'; // 查询新闻列表
  private addNewshostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'ofWeb/news/add'; // 新增新闻列表
  private delNewshostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'ofWeb/news/delete'; // 删除新闻列表
  private upadateNewshostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'ofWeb/news/update'; // 修改新闻列表
  private goIndexhostUrl = ParamConfig.HTTP_WEB_HOST_URL + 'ofWeb/news/indexDisplay'; // 添加到首页
  constructor(private http: Http, private router: Router) {
  };

  // 查询新闻
  getNews(page): Promise<any> {
    const request = JSON.stringify({
      rows: ParamConfig.DATA_LIST_PAGE_SIZE,
      page: page,
      criteria: {'newsTitle': ''}
    });
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http
      .post(this.getNewhostUrl, request, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === 1) {
            const pagedata = new AddNewsPage();
            pagedata.addNews = body as AddNews[];
            pagedata.totalPages = body.total;
            return pagedata;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }
  // 新增新闻
  addNewsInfo(addNews: AddNews, imgUrl): Promise<any> {
    addNews.newsIndexDisplay = addNews.iftop === true ? '1' : '0'; // 给后台传1或者0
    const request = JSON.stringify({
      newsTitle: addNews.newsTitle,
      newsUrl: addNews.newsUrl,
      newsMainpicurl: imgUrl,
      newsContent: addNews.newsContent,
      newsIndexDisplay: addNews.newsIndexDisplay
    });
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http
      .post(this.addNewshostUrl, request, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === '1') {
            return true;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }
  // 修改新闻
  updateNewsinfo(addNews: AddNews, imgUrl): Promise<any> {
    const request = JSON.stringify({
      newsId: addNews.newsId,
      newsTitle: addNews.newsTitle,
      newsContent: addNews.newsContent,
      newsUrl: addNews.newsUrl,
      newsMainpicurl: imgUrl
    });
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http
      .post(this.upadateNewshostUrl, request, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === '1') {
            return true;
          }
        }
      )
      .catch(this.handleError);
  }
  // 删除事件
  removeNewsinfo(addNews: AddNews): Promise<any> {
    const request = addNews.newsId;
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http
      .get(this.delNewshostUrl + '?newsId=' + request, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === '1') {
            return true;
          }
          if (body.status === 2) {
            alert(body.msg);
            this.router.navigate(['/login']);
          }
        }
      )
      .catch(this.handleError);
  }
  // 添加到首页
  goNewsIndex(addNews: AddNews): Promise<any> {
    const request = JSON.stringify({
      newsId: addNews.newsId,
      newsIndexDisplay: '1'
    });
    const headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http
      .post(this.goIndexhostUrl, request, {headers: headers})
      .toPromise()
      .then(
        res => {
          const body = res.json();
          if (body.status === '1') {
            return true;
          }
        }
      )
      .catch(this.handleError);
  }
  // 错误处理
  private handleError(error: any): Promise<[boolean, any]> {
    console.error('An error occurred', error); // for demo purposes only
    return Promise.reject([false, error.toString()]);
  }
}
